#include "key.h"
#include "work.h"
#include "iap.h"
#include "timer.h"

uint8_t k1_cnt=250,k2_cnt=250,boot_cnt=0,life_cnt=0,age_cnt=0;
uint8_t k3_cnt=250;
boolean_t Touch_OK=0;
extern uint8_t Work_Mode,Trigger_State,Work_Strength,phase_cnt;
extern uint8_t ms10_cnt,off_cnt1,flash_cnt,Error_State,off_cnt;
extern boolean_t Voltage_OK,Trigger_Continuous;
extern boolean_t Trigger_OK,Trigger_f,EMS_Load,ems_f;
extern boolean_t Boot_Mode,age_mode,life_mode,flash_f;
extern uint8_t Beep_Time,period_cnt1;
extern uint16_t period_cnt,Trigger_Cnt;

/*************************************************************************************************
 * JR10 进入老化/测试模式 方法
 * 1. 老化测试： 按住打光键，接入适配器。再长按开关机键进入老化程序，  长按打光按键进行测试[打500发，快闪300发，慢闪200发]
 * 2. 寿命测试： 按住开关机键，接入适配器，再长按打光键，进入寿命测试。长按打光按键进行测试[模式1档位1 300发，模式2档位6闪 100发, 停止8min后再循环]
 * 
 ** JP3 进入老化/测试模式 方法
 * 1. 老化测试： 按住打光键，接入适配器。再长按开关机键进入老化程序，  长按打光按键进行测试, 打完500发后关机
 * 								[[模式2 3档200发] + [模式3 1档100发] + [2档100发] + [3档100发] == 500发]
 * 2. 寿命测试： 按住开/关机键，接入适配器，再长按打光键，进入寿命测试。长按打光按键进行测试，打完500发，休息5min，然后循环执行
 * 								[模式3档位[1] 200发 + 档位[2] 200发 + 档位[3]100发,{大约耗时2min20秒} + 休息5min后再循环]
 * 
***************************************************************************************/
void Key_Scan()
{
	uint32_t n=0;

	// [开/关机][模式]
	if(K3 == 0){
		if(k2_cnt < 250){
			k2_cnt++;
		}
				
		if(k2_cnt == 200){
			if(Work_Mode){
				Work_Mode = 0;
				Beep_Time = 3;
			}
			else{
				if(Error_State == 0){
					if(age_cnt == 0){
						age_mode = 1;
						Work_Mode = 3;	// 老化模式，直接进入模式3，进行测试
						Work_Strength = 2;
					}
					else{
						Work_Mode = 1;
					}
					Beep_Time = 3;
					off_cnt1 = 0;
					off_cnt = 99;
					flash_cnt = 0;
					flash_f = 0;
				}
			}
		}
	}
	else{
		if((k2_cnt > 3) && (k2_cnt <= 100)){
			if(Work_Mode){
				Work_Mode++;
				if(Work_Mode > 3){
					Work_Mode = 1;
				}
				period_cnt = 0;
				period_cnt1 = 0;
				phase_cnt = 0;
				ems_f = 0;
				Beep_Time = 1;
				Work_Strength = 0;
				Trigger_Continuous = 0;
				Trigger_State = Trigger_End;
				off_cnt1 = 0;
				flash_cnt = 0;
				flash_f = 0;
			}
			else{
				if(k1_cnt == 250){
					boot_cnt++;
				}
				else{
					boot_cnt = 0;
				}
				if(boot_cnt > 10){
					Boot_Mode = 1;
					Work_Mode = 0;
				}
			}
		}
		k2_cnt = 0;
		if(life_cnt < 250){
			life_cnt++;
		}
				
	}
	// 档位按键
	if(K2 == 0){
		if(k3_cnt<250){
			k3_cnt++;
		}
		
		if(k3_cnt==200){

		}
	}
	else{
		if((k3_cnt > 3) && (k3_cnt < 100)){
			if(Work_Mode){
				Work_Strength++;
				if(Work_Strength > 2){
					Work_Strength = 0;
				}
				
				Trigger_Continuous = 0;
				Trigger_State = Trigger_End;
				off_cnt1 = 0;
				Beep_Time = 1;
			}
		}
		k3_cnt = 0;
	}
	// 打光按键
	if(K1 == 0){
		if(k1_cnt < 250){
			k1_cnt++;
		}
		if(k1_cnt == 200){
			if(Work_Mode == 2||Work_Mode == 3){
				if(Trigger_Continuous){
					Trigger_Continuous = 0;
					Trigger_State = Trigger_End;
				}
				else{
					Trigger_Continuous = 1;
				}
				Trigger_Cnt = 0;
				off_cnt1 = 0;
				Beep_Time = 1;
			}
			if(Work_Mode == 0){
				if(life_cnt == 0){
					life_mode = 1;
					
					Work_Mode = 3;
					Work_Strength = 0;
					Beep_Time = 3;
					off_cnt1 = 0;
					off_cnt = 99;
				}
			}
		}
	}
	else{
		if((k1_cnt > 3) && (k1_cnt <= 100)){
			if((Work_Mode == 2) || (Work_Mode == 3)){
				printf("k1_short Voltage_OK:%d,touch_flag[%d], Trigger_Continuous:[%d] \r\n",Voltage_OK, Touch_OK, Trigger_Continuous);

				if((Voltage_OK == 1) && (Touch_OK == 1) && (Trigger_Continuous == 0)){
					// if(life_mode||age_mode){	// 测试模式，短按，即可连闪
					// 	Trigger_Continuous = 1;
					// }

					if(Trigger_State==Trigger_End){
						Trigger_State=Trigger_Start;
						Trigger_Cnt=0;
						printf("k1_short Trigger_Cnt:%d  \r\n",Trigger_Cnt);
						Trigger_OK=1;
						ms10_cnt=0;
						off_cnt1=0;
					}
				}
			}
		}
		k1_cnt = 0;
		if(age_cnt < 250){
			age_cnt++;
		}
	}
//		Touch_OK=1;
}

